<?php

class AdminBrachcesController extends BaseController
{
    /**
     * Place Model
     * @var Place
     */
    protected $place;

    protected $branch;

    /**
     * Inject the models.
     * @param Post $post
     */
    public function __construct(Place $place, ProvincePlaces $branch)
    {
        parent::__construct();
        $this->place = $place;
        $this->branch = $branch;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        // Grab all the groups
        $places = $this->place->all();
        $provinces = Province::all();
        $branches = $this->branch
                    ->with(array('place', 'province'))
                    ->paginate(20);

        // Show the page
        return View::make('admin/branches/index', compact('places', 'provinces', 'branches'));
    }

    /**
     * Show the form for creating a new resource.
     * @param  ProvincePlaces $branch Province Place model
     * @return Response
     */
    public function getEdit($branch)
    {
        $provinces = Province::all();
        $places = Place::all();
        $branch = ProvincePlaces::with('Province', 'Place')->where('id', $branch)->first();

        return View::make('admin/branches/edit', compact('provinces', 'places', 'branch'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function postEdit($branchId)
    {
        $branch = ProvincePlaces::find($branchId);

        $rules = array(
            'branch_name'   => 'required|min:3',
            'logo' => 'mimes:jpeg,bmp,png,gif|max:3000',
            'email' => 'email',
            'website' => 'url'
        );
        $validator = Validator::make(Input::all(), $rules);

        if ($validator->passes()) {

            $file = Input::file('image');
            if ($file) {

                $originalName = $file->getClientOriginalName();
                $filename = Str::random(36) .'.'. File::extension($originalName);

                if ($file->move(base_path().'/public/uploads/images/branches/', $filename)) {

                    // Delete old file
                    File::delete(base_path().'/public/uploads/images/branches/' . $branch->branch_logo);
                    File::delete(base_path().'/public/uploads/images/branches/thumb/' . $branch->branch_logo);

                    $branch->branch_logo =  $filename;
                    Image::make(base_path().'/public/uploads/images/branches/'. $filename)
                    ->resize(220, null, true)
                    ->save(base_path().'/public/uploads/images/branches/thumb/'. $filename);
                }
            }

            $branch->branch_name = Input::get('branch_name');

            // Address
            $branch->house = Input::get('house');
            $branch->street = Input::get('street');
            $branch->sangkat = Input::get('commune');
            $branch->khan = Input::get('khan');
            $branch->phone = Input::get('phone');
            $branch->email = Input::get('email');
            $branch->website = Input::get('website');
            $branch->lat = Input::get('lat');
            $branch->lng = Input::get('lng');

            //TODO: for tmp
            $branch->branch_logo = Input::get('branch_logo');

            if ($branch->save()) {
                // Redirect to the new blog post page
                return Redirect::to('admin/branches/' . $branch->id . '/edit')->with('success', 'Brach has been updated.');
            }

            // Redirect to the place create page
            return Redirect::to('admin/branches/' . $branch->id . '/edit')->with('error', 'Brach does not update. Please try again');

        }
        // Form validation failed
        return Redirect::to('admin/branches/' . $branch->id . '/edit')->withInput()->withErrors($validator);
    }

    public function getBankByProvince($province_id)
    {
        $branches =  $this->branch
                ->with('province', 'place')
                ->where('province_id', $province_id)
                ->get();

        return Response::json($branches);
    }
}
